package logic

import (
	"context"
	"errors"
	"go.uber.org/zap"
	"smart-health-platforms/config"
	"smart-health-platforms/pkg"
	"user_srv/internal/dao"
	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendChatSimpleLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendChatSimpleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendChatSimpleLogic {
	return &SendChatSimpleLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendChatSimpleLogic) SendChatSimple(in *user.SimpleChatRequest) (*user.SimpleChatResponse, error) {
	if in.Content == "" {
		return nil, errors.New("请输入内容")
	}

	// 2. 读取配置
	c := l.svcCtx.Config.TongYi
	if c.ApiKey == "" {
		return nil, errors.New("请配置通义AI的ApiKey")
	}

	// 3. 调用 dao 层（导航层只做这一件事）
	reply, err := dao.NewChatDao(config.DB).AskAndSave(l.ctx, 1, in.Content, c.Model, c.ApiKey, c.Url)

	if err != nil {
		return nil, errors.New("调用通义AI失败" + err.Error())
	}
	pkg.Warn("智能医生回答成功",
		zap.String("reply", reply))
	// 4. 返回响应
	return &user.SimpleChatResponse{Reply: reply}, nil
}
